package com.wind.base.http;

import android.text.TextUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

/**
 * Created by wind on 2018/1/8.
 */

public class HeaderParams {
    public static final String KEY_APP_PKG = "appId";//包名
    public static final String KEY_APP_VERSION = "appVersion";//api版本号
    public static final String KEY_DEVICE_FACTION = "devices";//设备信息
    public static final String KEY_OS = "os";//操作系统 （ios,android）
    public static final String KEY_OSVERSION = "osVersion";// 操作系统版本号（9.3.5）
    public static final String KEY_APP_CHANNEL = "channel";//渠道名称
    public static final String KEY_TOKEN = "token";
    public static final String KEY_MODE = "curmode";//yes-审核模式 no-线上模式 test-测试模式
    public static final String KEY_TIMEZONE = "timezone";//yes-审核模式 no-线上模式 test-测试模式



    /*public static final String KEY_LONGITUDE="longitude";
    public static final String KEY_LATITUDE="latitude";*/

    public static final String DEFAULT_TOKEN = "default";

    private String pkg;
    private String apiVersion;
    private String channel;
    private String deviceName;
    private String osVersion;
    private HeaderParams(String pkg, String apiVersion,String osVersion,String channel, String deviceName) {
        this.pkg = pkg;
        this.apiVersion = apiVersion;
        this.osVersion=osVersion;
        this.channel = channel;
        this.deviceName=deviceName;
    }

    private Map<String, String> mHeaders;


    public Map<String, String> getHeaders() {
        if (mHeaders == null) {
            return getHeaders(DEFAULT_TOKEN);
        }
        return mHeaders;

    }

    public synchronized Map<String, String> getHeaders(String token) {
        /*String latitude= PrefsUtil.getString(Globe.sAppContext, C.PREF_KEY.LOCATION_LATITUDE,"");
        String longitude=PrefsUtil.getString(Globe.sAppContext,C.PREF_KEY.LOCATION_LONGITUDE,"");*/
        if (mHeaders == null) {
            if (TextUtils.isEmpty(token)) {
                token = DEFAULT_TOKEN;
            }
            mHeaders = new HashMap<>();
            mHeaders.put(KEY_APP_PKG, pkg);
            mHeaders.put(KEY_APP_VERSION, apiVersion);
            mHeaders.put(KEY_DEVICE_FACTION,deviceName );

            mHeaders.put(KEY_APP_CHANNEL, channel);
            mHeaders.put(KEY_TOKEN, token);

            mHeaders.put(KEY_OS,"android" );
            mHeaders.put(KEY_OSVERSION,osVersion);
            //todo 计算signature
           /* List<String> params = new ArrayList<>();
            params.add(timestamp);
            params.add(channel);
            //params.add(C.Value.SECRET_KEY);
            params.add(secretKey);
            String signature = EncryptUtil.encryptBysha1(EncryptUtil.lexicographicalOrderSort(params));

            mHeaders.put(KEY_SIGNATURE, signature);*/
            String curMode= "0";//PrefsUtil.getString(Globe.sAppContext, C.PREF_KEY.CONFIG_CURMODE,"");
            mHeaders.put(KEY_MODE,curMode);
            mHeaders.put(KEY_TIMEZONE, TimeZone.getDefault().getID());
        } else {
            String existToken = mHeaders.get(KEY_TOKEN);
            if (!existToken.equals(token)) {
                mHeaders.put(KEY_TOKEN, token);
            }
        }

        return mHeaders;
    }

    public static class Builder {
        private String pkg;
        private String apiVersion;
        private String osVersion;
        private String channel;
        private String deviceName;

        public Builder deviceName(String deviceName) {
            this.deviceName = deviceName;
            return this;
        }

        public Builder pkg(String pkg) {
            this.pkg = pkg;
            return this;
        }


        public Builder appVersion(String appVersion) {
            this.apiVersion = appVersion;
            return this;
        }
        public Builder osVersion(String osVersion) {
            this.osVersion = osVersion;
            return this;
        }

        public Builder channel(String channel) {
            this.channel = channel;
            return this;
        }


        public HeaderParams build() {
            HeaderParams params = new HeaderParams(pkg, apiVersion, osVersion, channel,deviceName);
            return params;

        }
    }

}
